package org.morningangel.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.NamingException;

import org.morningangel.bean.Remind;
import org.morningangel.dao.RemindDao;
import org.morningangel.util.DBHelperUtil;

public class RemindDaoImpl implements RemindDao {

	@Override
	public Remind getRemindTimeById(Integer userId) {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Remind rm=new Remind();
		String sql="select a.userId,a.remindTime from tb_remind a where a.userId=?";
		try {
			conn=DBHelperUtil.getConnection();
			
			ps=conn.prepareStatement(sql);
			ps.setInt(1,userId);
		    rs=ps.executeQuery();
		    if(rs.next()){
		    	rm.setUserId(rs.getInt(1));
		    	rm.setRemindTime(rs.getInt(2));
		    }
		    
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DBHelperUtil.close(rs, ps, conn);
		return rm;
	}

	@Override
	public Integer updateRemindTime(Integer userId, Integer remindTime) {
		Connection conn=null;
		PreparedStatement ps=null;
		String sql="update tb_remind set remindTime=? where userId=?";
		Integer b=0;
		try {
			conn=DBHelperUtil.getConnection();
			conn.setAutoCommit(false);
			ps=conn.prepareStatement(sql);
			ps.setInt(1, remindTime);
			ps.setInt(2, userId);
			
			b=ps.executeUpdate();
			conn.commit();
			conn.setAutoCommit(true);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DBHelperUtil.close(ps, conn);
		return b;
	}

}
